package com.jlcloud.core.oauth2.provider;

import com.jlcloud.core.oauth2.constant.OAuth2ResponseConstant;
import lombok.Data;
import com.jlcloud.core.tool.support.Kv;

import java.io.Serial;
import java.io.Serializable;

/**
 * OAuth2Response
 *
 * @author By: JlCloud <br>
 * @Package: com.jlcloud <br>
 * @CreateTime: 2024-12-03 21:45 <br>
 * @Copyright: 2021 www.jilianjituan.com Inc. All rights reserved. <br>
 * @Caution 注意：本内容仅限于冀联人力集团内部传阅，禁止外泄以及用于其他的商业目的 <br>
 */
@Data
public class OAuth2Response implements Serializable {

	@Serial
	private static final long serialVersionUID = 1L;

	/**
	 * 实例化
	 */
	public static OAuth2Response create() {
		return new OAuth2Response();
	}

	/**
	 * 响应参数
	 */
	private Kv args = Kv.create();

	public Kv ofFailure(int errorCode, String errorDescription) {
		args.set(OAuth2ResponseConstant.SUCCESS, Boolean.FALSE);
		args.set(OAuth2ResponseConstant.ERROR_CODE, errorCode);
		args.set(OAuth2ResponseConstant.ERROR_DESCRIPTION, errorDescription);
		return args;
	}

	public Kv ofSuccessful(String successDescription) {
		args.set(OAuth2ResponseConstant.SUCCESS, Boolean.TRUE);
		args.set(OAuth2ResponseConstant.SUCCESS_CODE, OAuth2ResponseConstant.DEFAULT_SUCCESS_CODE);
		args.set(OAuth2ResponseConstant.SUCCESS_DESCRIPTION, successDescription);
		return args;
	}

	public Kv ofSuccessful(int successCode, String successDescription) {
		args.set(OAuth2ResponseConstant.SUCCESS, Boolean.TRUE);
		args.set(OAuth2ResponseConstant.SUCCESS_CODE, successCode);
		args.set(OAuth2ResponseConstant.SUCCESS_DESCRIPTION, successDescription);
		return args;
	}

	public Kv ofSuccessful(int successCode, String successDescription, Kv successData) {
		args.set(OAuth2ResponseConstant.SUCCESS, Boolean.TRUE);
		args.set(OAuth2ResponseConstant.SUCCESS_CODE, successCode);
		args.set(OAuth2ResponseConstant.SUCCESS_DESCRIPTION, successDescription);
		args.setAll(successData);
		return args;
	}

	public Kv ofValidation(OAuth2Validation validation) {
		args.set(OAuth2ResponseConstant.SUCCESS, validation.isSuccess());
		args.set(OAuth2ResponseConstant.ERROR_CODE, validation.getCode());
		args.set(OAuth2ResponseConstant.ERROR_DESCRIPTION, validation.getMessage());
		return args;
	}
}
